package utils;

import common.ClientEntity;


public class ClientServerMessage
{
	public static final String MESSAGE_DELIMITER = "\t\t\t";

	public static final String MESSAGE_DELIMITER_SHORT = "\t\t";
	
	protected ClientServerEnum messageType;
	protected String messageData;
	public ClientServerMessage(ClientServerEnum msgType, String msgData)
	{	
		messageType = msgType;
		messageData = msgData;
	}
	
	public ClientServerMessage(String msg) {
		String [] msgSplit = msg.split(MESSAGE_DELIMITER);
		this.messageType = ClientServerEnum.ConvertFromInt(Integer.parseInt(msgSplit[0]));
		if(msgSplit.length > 1)
		{
			this.messageData = msgSplit[1];
		}
	}
	public static String getContentOutput(int ordinal, String content)
	{
		return ordinal + MESSAGE_DELIMITER + content+"\n";
	}
	public static String getContentOutput(int ordinal,String arg1, String arg2)
	{
		return ordinal + MESSAGE_DELIMITER + arg1 + MESSAGE_DELIMITER_SHORT+arg2+"\n";
	}
	
	public ClientServerEnum getMessageType() {
		return messageType;
	}
	
	public String getMessage() {
		return "" + messageType.ordinal() + MESSAGE_DELIMITER + messageData +"\n";
	}
	
	public static String constructMessage(int clientId, String content) {
		return ""+ clientId + " " + content + "\n";
	}
	
	public int getClientId() {
		String[] parts = messageData.split(" ");
		return Integer.parseInt(parts[0]);
	}
	
	public String getPassword() {
		return getContent();
	}
	public String getSendClientContent()
	{
		return this.messageData.split(MESSAGE_DELIMITER_SHORT)[1];
	}
	public int getSendClientID()
	{
		return Integer.parseInt(this.messageData.split(MESSAGE_DELIMITER_SHORT)[0]);
	}
	
	public String getContent() {
		int firstSpace = messageData.indexOf(' ');
		return messageData.substring(firstSpace+1);
	}
	
	public static String getLoginMessage(ClientEntity c) {
		String res = "" + ClientServerEnum.LOGIN.ordinal() + MESSAGE_DELIMITER +
			c.getId() + " " + c.getPassword() + "\n";
		return res;
	}
	
}